var countImage = 1;
function changeFile(obj){
	var image = $(obj).val();
	var flag = "(.jpg|.JPG|.png|.PNG)$";
	 var re=new RegExp(flag);
	if (!re.test(image)) {
		
		alert("图片格式必须png或jpg");
		$(obj).val('');
	}
	var orimage = document.getElementById("orImage_"+countImage).files;
	var thimage = document.getElementById("thImage_"+countImage).files;
	if(countImage >1){
		var countImageBack = countImage - 1;
		var orimage_back = document.getElementById("orImage_"+countImageBack).files;
		var thimage_back = document.getElementById("thImage_"+countImageBack).files;
	}
	//判断最后2个图片是否为空
	if(orimage.length >0 &&thimage.length >0){
		var _this = $("#thImage_"+countImage);
		var tdNode = _this.parent();
		var trNode = tdNode.parent();
		//判断是否还有下一个tr
		if(!trNode.next().html()) {
			countImage = countImage+1;
			trNode.parent().append('<tr><td>图片</td><td><input type="file" name="orImage_'+countImage+'" id="orImage_'+countImage+'" multiple="true" onchange="changeFile(this)"  class="file ui-widget-content ui-corner-all"></td> </tr>');
			trNode.parent().append('<tr><td>缩图</td><td><input type="file" name="thImage_'+countImage+'" id="thImage_'+countImage+'" multiple="true" onchange="changeFile(this)" class="file ui-widget-content ui-corner-all"></td></tr>');
		}
		//最后一个全为空则删除最后一个tr,不删除最后一个
	}else if(orimage.length ==0 &&thimage.length ==0  && countImage != 1 &&orimage_back.length ==0 && thimage_back.length == 0){
		var _this = $("#orImage_"+countImage);
		
		var tdNode = _this.parent();
		var trNode = tdNode.parent();
		var trNode_2 = trNode.next();
		trNode.remove();
		trNode_2.remove();
		countImage = countImage-1;
	}
}
function checkNum(obj,name){
	var regexp = /^[1-9]\d*$/;
    if ( !( regexp.test($(obj).val() ) ) && $(obj).val() != '' ) {
    	alert(name+"必须为数字");
    	return false;
    }
    return true;
}

function deleteImage(obj){
	var param = {
			id : $(obj).attr("id"),
			image : $(obj).attr("name")
		};

	API.deleteDrugImage(param, function(res) {
		UTIL.releaseButton(obj);
		if (res.code == '0') {
			window.location.reload();
		} else {
			alert(res.desc);
			window.location.reload();
		}
	});
}

function upload(obj,type) {
    var formData = new FormData();  
    for(var i = 1;i <= countImage; i++){
        var orImage = document.getElementById("orImage_"+i).files;
        var thImage = document.getElementById("thImage_"+i).files;
        if(orImage.length>0 && thImage.length>0){//只要有图片必须全部都填
        	formData.append("orImage_"+i,orImage[0]);
        	formData.append("thImage_"+i,thImage[0]);
        }else if(orImage.length == 0 &&  thImage.length ==0){//可以都不填
    		countImage = countImage-1;
        }else{
        	alert("有图片必须有缩图!");
        	return;
        }
    }
    if(!(checkNum("#status","状态") &&　checkNum("#pharmacyId","药给力药品ID"))){
    	return;
    }
	formData.append("status",$("#status").val());
    formData.append("approvalDoc",$("#approvalDoc").val());
    formData.append("drugName",$("#drugName").val());
    formData.append("produceAddr",$("#produceAddr").val());
    formData.append("syrq",$("#syrq").val());
    formData.append("price",$("#price").val());
    formData.append("buyCity",$("#buyCity").val());
    formData.append("prescriptionFlag",$("#prescriptionFlag").val());
    formData.append("supportInsurance",$("#supportInsurance").val());
    formData.append("sfxy",$("#sfxy").val());
    formData.append("pharmacyId",$("#pharmacyId").val());
    formData.append("buyFlag",$("#buyFlag").val());
    formData.append("component",$("#component").val());
    formData.append("drugUsage",$("#drugUsage").val());
    formData.append("indFun",$("#indFun").val());
    formData.append("specification",$("#specification").val());
    formData.append("adverseEffect",$("#adverseEffect").val());
    formData.append("specialUse",$("#specialUse").val());
    formData.append("taboo",$("#taboo").val());
    formData.append("drugInteraction",$("#drugInteraction").val());
    formData.append("yfyy",$("#yfyy").val());
    formData.append("etyy",$("#etyy").val());
    formData.append("lnyy",$("#lnyy").val());
    formData.append("count",countImage);
    if($("#id")){
    	formData.append("id",$("#id").val());
    }

	$.ajax({
		url : "/medicine/drug_edit.do",
        type : 'post',
        data : formData,
        cache : false,
        processData : false, // 不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
        contentType : false,
		success : function(res) {
			if (res.code == '0') {
				window.location.href = "/medicine/drug_list.htm";
			} else {
				alert(res.desc);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if ($("#errorRequestStatus")) {
				$("#errorRequestStatus").val(XMLHttpRequest.status);
			}
			if ($("#errorRequestReadyState")) {
				$("#errorRequestReadyState").val(XMLHttpRequest.readyState);
			}
			if ($("#errorTextStatus")) {
				$("#errorTextStatus").val(textStatus);
			}
			// 如果在一个方法调用未结束直接切换页面会包状态为0的错误，屏蔽掉
			if (XMLHttpRequest.status != 0) {
				alert('系统异常，请与管理员联系');
			}
		},
		complete : function() {
		}
	});
}



